import xml.dom.minidom

from Learn.xml.Movie import Movie


def parseXml(movieList):
    domTree = xml.dom.minidom.parse("movies.xml")
    collection = domTree.documentElement
    if collection.hasAttribute("shelf"):
        print("Root element : %s " % collection.getAttribute("shelf"))

    # 获取所有节点
    movies = collection.getElementsByTagName("movie")
    print("*****Movie*****")
    for movie in movies:
        data = Movie()
        if movie.hasAttribute("title"):
            data.title = movie.getAttribute("title")
        type = movie.getElementsByTagName("type")[0]
        format = movie.getElementsByTagName("format")[0]
        rating = movie.getElementsByTagName("rating")[0]
        description = movie.getElementsByTagName("description")[0]
        data.type = type.childNodes[0].data
        data.format = format.childNodes[0].data
        data.rating = rating.childNodes[0].data
        data.description = description.childNodes[0].data
        movieList.append(data)


if __name__ == "__main__":
    movieList = []
    parseXml(movieList)
    for movie in movieList:
        print(repr(movie))
        print(movie.__dict__)

